﻿using System.Collections.Generic;
using System.Windows.Forms;
using GeoSketch2.Core;

namespace GeoSketch2.Archaeo
{
    /// <summary>
    /// The tools to display information about an object
    /// </summary>
    public class TObjectInfos:BaseArchaeoTool
    {
        #region Fields
        List<BaseArchaeoDrawableObject> CommentedObjects;
        ObjectInfosForm InfoForm; 
        #endregion

        #region Constructor
        /// <summary>
        /// The base class for all object to be drawn to the screen
        /// </summary>
        /// <param name="Viewport">The viewport</param> 
        public TObjectInfos(Viewport Viewport)
            : base(Viewport,"Infos")
        {
            CommentedObjects = new List<BaseArchaeoDrawableObject>();
            InfoForm = new ObjectInfosForm(this);
            SearchCommented();
        }
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            double is_near_distance_object_coordinates = Viewport.Object2ImageTransformation.InverseTransform(Core.Vars.Setup.Tools.TSelection.IsNearDistance);
            Point2D64D clic_object_coordinates = Viewport.Object2ImageTransformation.InverseTransform(MouseArgs.Location);

            foreach (BaseArchaeoDrawableObject obj in Viewport.GeoObjects)
            {
                if (obj.Layer.Visible)
                {
                    if (obj.IsNear(clic_object_coordinates, is_near_distance_object_coordinates))
                    {
                        InfoForm.Object = obj;
                        InfoForm.Show();
                        break;
                    }
                }
            }
            RefreshViewport();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Searches the commented object to highlight them
        /// </summary>
        internal void SearchCommented()
        {
            CommentedObjects.Clear();

            foreach (BaseArchaeoDrawableObject obj in Viewport.GeoObjects)
            {
                if (obj.Layer.Visible)
                {
                    if (obj.Comment != "")
                    {
                        CommentedObjects.Add(obj);
                    }
                }
            }
            RefreshViewport();
        }

        /// <summary>
        /// Draw the graphics requested by the object
        /// </summary>
        public override void Draw()
        {
            Archaeo.Viewport vp = Viewport;

            foreach (BaseArchaeoDrawableObject obj in CommentedObjects)
            {
                obj.DrawCustomPen(vp, Vars.Setup.Tools.TObjectInfos.HighlightPen);
            }
        }

        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Click an object to get information");
        } 
        #endregion
    }
}
